import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DBTestsPartners {

	/*
	 * Preconditions: 
	 * Existing Table: PARTNERS
	 * Existing instert: insert into partners values('PORR AG', 'Beton', 'Bahnallee 23, 2148 DeutschWagram', 'wmpm18@localhost', '02275/984563', 'Hr. Mustermann', '');
	 * 
	 * As these Table only contains information that has to be read and is not changed, only select-commands are tested
	 */
	private Connection connection;

	@Before
	public void connect()
	{
		try
		{
			connection = DriverManager.getConnection("jdbc:h2:tcp://localhost/activiti", "sa", "");
		}
		catch (SQLException e)
		{
			e.printStackTrace();
			fail();
		}
	}

	@Test
	public void testSelectAllPartners()
	{
		try
		{
			PreparedStatement ps=connection.prepareStatement("select * from PARTNERS");
			ResultSet rs = ps.executeQuery();

			ArrayList<String> partners = new ArrayList<String>();
			while(rs.next()){
				partners.add(rs.getString("COMPANY_"));
			}

			assertEquals(7, partners.size());

			rs.close();
			ps.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			fail();
		}
	}

	@Test
	public void selectOnePartner()
	{
		try
		{
			PreparedStatement ps=connection.prepareStatement("select * from PARTNERS where COMPANY_='PORR AG'");
			ResultSet rs = ps.executeQuery();

			if(rs.next())
			{
				assertEquals("PORR AG", rs.getString("COMPANY_"));
				assertEquals("Beton", rs.getString("OFFER_"));
				assertEquals("Bahnallee 23, 2148 DeutschWagram", rs.getString("ADDRESS_"));
				assertEquals("wmpm18@localhost", rs.getString("E_Mail_"));
				assertEquals("02275/984563", rs.getString("PHONE_"));
				assertEquals("Hr. Mustermann", rs.getString("CONTACT_"));
			}

			rs.close();
			ps.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			fail();
		}
	}

	@After
	public void closeConnection()
	{
		try
		{
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			fail();
		}
	}
}
